package org.zyx.item.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.zyx.item.bo.SpuBo;
import org.zyx.item.entity.Sku;
import org.zyx.item.entity.Spu;
import org.zyx.item.entity.SpuDetail;
import org.zyx.item.service.GoodsService;
import org.zyx.yiyou.common.entity.PageResult;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: SwordMaple
 * @Date: 2020/9/7  20:47
 * @Description: 商品信息控制器
 * @Version: 1.0
 */
@RestController
@RequestMapping
public class GoodsHandler {

    @Resource
    private GoodsService goodsService;

    /**
     * 根据条件分页查询spu
     * @param key
     * @param saleable
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuBo>> findSpuByPage(
            @RequestParam(name = "key", required = false) String key,
            @RequestParam(name = "saleable", required = false) Boolean saleable,
            @RequestParam(name = "page", defaultValue = "1")Integer page,
            @RequestParam(name = "rows", defaultValue = "5")Integer rows){

        PageResult<SpuBo> pageResult = goodsService.findSpuByPage(key,saleable,page,rows);

        if(pageResult == null || CollectionUtils.isEmpty(pageResult.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 添加商品
     * @param spuBo spu拓展类(业务对象),包含商品相关信息
     * @return
     */
    @PostMapping("/goods")
    public ResponseEntity<Void> addGoods(@RequestBody SpuBo spuBo){
        goodsService.saveGoods(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();//新增时的响应码
    }

    /**
     * 编辑商品
     * @param spuBo
     * @return
     */
    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo){
        goodsService.updateGoods(spuBo);
        return ResponseEntity.noContent().build();//更新时的响应码204
    }

    /**
     * 通过SpuId查询SpuDetail
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> findSpuDetailById(@PathVariable("spuId") Long spuId){

        SpuDetail spuDetail = goodsService.findSpuDetailById(spuId);
        if(ObjectUtils.isEmpty(spuDetail)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuDetail);
    }

    /**
     * 根据spuId查询skus
     * @param id
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<Sku>> findSkusBySpuId(Long id){

        List<Sku> skus = goodsService.findSkusBySpuId(id);
        if(CollectionUtils.isEmpty(skus)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(skus);
    }

    /**
     *  删除方法未添加事件,后续添加
     *  逻辑删除:更新valid字段:0删除(false),1存在(true)
     */
    @DeleteMapping("/spu/delete/{spuId}")
    public ResponseEntity<Void> deleteById(@PathVariable("spuId")Long spuId){

        System.out.println(spuId);
        //执行逻辑删除,还需要修改查询spu时的valid值


        return ResponseEntity.ok().build();
    }

    /**
     * 根据id查询spu
     * @param spuId
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<Spu> findSpuById(@PathVariable("id") Long spuId){
        Spu spu = goodsService.findSpuById(spuId);
        if (ObjectUtils.isEmpty(spu)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spu);
    }

    /**
     * 通过skuId查找sku
     * @param id
     * @return
     */
    @GetMapping("/sku/{id}")
    public ResponseEntity<Sku> querySkuById(@PathVariable("id") Long id){
        Sku sku = this.goodsService.querySkuById(id);
        if (sku == null){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(sku);
    }
}
